package p.ithorns.framework.common.reflect;

import sun.reflect.annotation.AnnotationParser;
import sun.reflect.annotation.AnnotationType;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;

/**
 * AnnotationUtil
 * TODO ithorns
 *
 * @author Ht.L
 * @date 2024-01-20 18:01
 * @since 1.0.0
 */
public class AnnotationUtil {

    private static <T extends Annotation> T parse(Map<String, Object> props, Class<T> clazz) {
        //获取注解默认属性
        AnnotationType instance = AnnotationType.getInstance(clazz);
        Map<String, Object> map = instance.memberDefaults();
        //  填充属性
        Set<Map.Entry<String, Object>> entries = props.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            if (map.containsKey(key)) {
                map.put(key, entry.getValue());
            }
        }

        //反射生成实例
        return (T) AnnotationParser.annotationForMap(clazz, map);
    }

}